package com.tencent.mm.opensdk.diffdev.a;

import android.annotation.SuppressLint;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Environment;
import android.util.Base64;
import android.util.Log;

import com.tencent.mm.opensdk.diffdev.OAuthErrCode;
import com.tencent.mm.opensdk.diffdev.OAuthListener;

import org.json.JSONObject;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

@SuppressLint("LongLogTag")
public final class d extends AsyncTask {
    static final class a {
        public OAuthErrCode t;
        public String u;
        public String v;
        public String w;
        public int x;
        public String y;
        public byte[] z;

        private a() {

        }

        public static a a(byte[] arg10) {
            String v1_1;
            a v0 = new a();
            if (arg10 != null && arg10.length != 0) {
                try {
                    v1_1 = new String(arg10, "utf-8");
                } catch (Exception v1) {
                    Log.e("MicroMsg.SDK.GetQRCodeResult", String.format("parse fail, build String fail, ex = %s", v1.getMessage()));
                    v0.t = OAuthErrCode.WechatAuth_Err_NormalErr;
                    return v0;
                }

                try {
                    JSONObject v2 = new JSONObject(v1_1);
                    int v1_2 = v2.getInt("errcode");
                    if (v1_2 != 0) {
                        Log.e("MicroMsg.SDK.GetQRCodeResult", String.format("resp errcode = %d", Integer.valueOf(v1_2)));
                        v0.t = OAuthErrCode.WechatAuth_Err_NormalErr;
                        v0.x = v1_2;
                        v0.y = v2.optString("errmsg");
                    } else {
                        v1_1 = v2.getJSONObject("qrcode").getString("qrcodebase64");
                        if (v1_1 != null && v1_1.length() != 0) {
                            byte[] v1_3 = Base64.decode(v1_1, 0);
                            if (v1_3 != null && v1_3.length != 0) {
                                if (d.b()) {
                                    File v3 = new File(d.c());
                                    v3.mkdirs();
                                    if (v3.exists()) {
                                        v3.delete();
                                    }

                                    if (!a.a(d.c(), v1_3)) {
                                        Log.e("MicroMsg.SDK.GetQRCodeResult", String.format("writeToFile fail, qrcodeBuf length = %d", Integer.valueOf(v1_3.length)));
                                        v0.t = OAuthErrCode.WechatAuth_Err_NormalErr;
                                        return v0;
                                    }

                                    v0.t = OAuthErrCode.WechatAuth_Err_OK;
                                    v0.w = d.c();
                                    v0.u = v2.getString("uuid");
                                    v0.v = v2.getString("appname");
                                    Log.d("MicroMsg.SDK.GetQRCodeResult", String.format("parse succ, save in external storage, uuid = %s, appname = %s, imgPath = %s", v0.u, v0.v, v0.w));
                                } else {
                                    v0.t = OAuthErrCode.WechatAuth_Err_OK;
                                    v0.z = v1_3;
                                    v0.u = v2.getString("uuid");
                                    v0.v = v2.getString("appname");
                                    Log.d("MicroMsg.SDK.GetQRCodeResult", String.format("parse succ, save in memory, uuid = %s, appname = %s, imgBufLength = %d", v0.u, v0.v, Integer.valueOf(v0.z.length)));
                                }

                                return v0;
                            }

                            Log.e("MicroMsg.SDK.GetQRCodeResult", "parse fail, qrcodeBuf is null");
                            v0.t = OAuthErrCode.WechatAuth_Err_JsonDecodeErr;
                            return v0;
                        }

                        Log.e("MicroMsg.SDK.GetQRCodeResult", "parse fail, qrcodeBase64 is null");
                        v0.t = OAuthErrCode.WechatAuth_Err_JsonDecodeErr;
                    }
                } catch (Exception v1) {
                    Log.e("MicroMsg.SDK.GetQRCodeResult", String.format("parse json fail, ex = %s", v1.getMessage()));
                    v0.t = OAuthErrCode.WechatAuth_Err_NormalErr;
                }
            } else {
                Log.e("MicroMsg.SDK.GetQRCodeResult", "parse fail, buf is null");
                v0.t = OAuthErrCode.WechatAuth_Err_NetworkErr;
            }

            return v0;
        }

        private static boolean a(String arg5, byte[] arg6) {
            FileOutputStream fout = null;
            FileOutputStream v2 = null;
            try {
                fout = new FileOutputStream(arg5);
                fout.write(arg6);
                fout.flush();
                Log.d("MicroMsg.SDK.GetQRCodeResult", "writeToFile ok!");
                return true;
            } catch (Exception v0_1) {
                Log.w("MicroMsg.SDK.GetQRCodeResult", "write to file error, exception:" + v0_1.getMessage());
                return false;
            } finally {
                try {
                    fout.close();
                } catch (IOException v0_2) {
                    Log.e("MicroMsg.SDK.GetQRCodeResult", "fout.close() exception:" + v0_2.getMessage());
                }
            }
        }
    }

    private String appId;
    private static final boolean m = !Environment.getExternalStorageState().equals("mounted") || !new File(Environment.getExternalStorageDirectory().getAbsolutePath()).canWrite() ? false : true;
    private static final String n = Environment.getExternalStorageDirectory().getAbsolutePath() + "/tencent/MicroMsg/oauth_qrcode.png";
    private static String o = "http://open.weixin.qq.com/connect/sdk/qrconnect?appid=%s&noncestr=%s&timestamp=%s&scope=%s&signature=%s";
    private String p;
    private String q;
    private OAuthListener r;
    private f s;
    private String scope;
    private String signature;

    public d(String arg1, String arg2, String arg3, String arg4, String arg5, OAuthListener arg6) {
        this.appId = arg1;
        this.scope = arg2;
        this.p = arg3;
        this.q = arg4;
        this.signature = arg5;
        this.r = arg6;
    }

    public final boolean a() {
        Log.i("MicroMsg.SDK.GetQRCodeTask", "cancelTask");
        boolean v0 = this.s == null ? this.cancel(true) : this.s.cancel(true);
        return v0;
    }

    static boolean b() {
        return d.m;
    }

    static String c() {
        return d.n;
    }

    protected final Object doInBackground(Object[] arg12) {
        Log.i("MicroMsg.SDK.GetQRCodeTask", "external storage available = " + d.m);
        String v0 = String.format(d.o, this.appId, this.p, this.q, this.scope, this.signature);
        long v2 = System.currentTimeMillis();
        byte[] v1 = e.a(v0, -1);
        Log.d("MicroMsg.SDK.GetQRCodeTask", String.format("doInBackground, url = %s, time consumed = %d(ms)", v0, Long.valueOf(System.currentTimeMillis() - v2)));
        return a.a(v1);
    }

    protected final void onPostExecute(Object arg6) {
        if (((a) arg6).t == OAuthErrCode.WechatAuth_Err_OK) {
            Log.d("MicroMsg.SDK.GetQRCodeTask", "onPostExecute, get qrcode success");
            this.r.onAuthGotQrcode(((a) arg6).w, ((a) arg6).z);
            this.s = new f(((a) arg6).u, this.r);
            f v0 = this.s;
            if (Build.VERSION.SDK_INT >= 11) {
                v0.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, new Object[0]);
            } else {
                v0.execute(new Object[0]);
            }
        } else {
            Log.e("MicroMsg.SDK.GetQRCodeTask", String.format("onPostExecute, get qrcode fail, OAuthErrCode = %s", ((a) arg6).t));
            this.r.onAuthFinish(((a) arg6).t, null);
        }
    }
}

